package alfred.proctice.com.recycleview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import me.drakeet.multitype.ItemViewBinder;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by Alfred on 2018/3/7.
 */

public class DymanicBinder extends ItemViewBinder<DymanicAllBean.Dymanic,DymanicBinder.ViewHolder> implements View.OnClickListener{
    private Context context;
    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        context = parent.getContext();
        return new ViewHolder(inflater.inflate(R.layout.item_dynamic,parent,false));
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull DymanicAllBean.Dymanic item) {
        GlideUtil.loadCircle(context,item.user.avatar,holder.img_header);
        holder.tv_name.setText(item.user.name);
        if (item == null){
            holder.tv_name.setText("");
            holder.tv_company.setText("");
        } else {
            holder.tv_name.setText(item.user.name);
            holder.tv_company.setText(StringUtils.info(" | ",item.user.companyShortName, item.user.position));
        }
        if (item.userLike) {
            holder.dynamic_zan.setIcon(R.drawable.dynamic_zan);
        } else {
            holder.dynamic_zan.setIcon(R.drawable.dynamic_zan_no);
        }
        holder.dynamic_zan.setOnClickListener(this);
        holder.dynamic_share.setOnClickListener(this);
        holder.dynamic_discuss.setOnClickListener(this);
        holder.dynamic_zan.setText(item.likeCount == 0 ? "" : item.likeCount + "");
        holder.dynamic_share.setText(item.shareCount == 0 ? "" : item.shareCount + "");
        holder.dynamic_discuss.setText(item.commentCount == 0 ? "" : item.commentCount + "");

        int type = item.topic.type;
        //type: 动态类型(1.动态 2.简历 3.招聘)
        if (type == 1) {
            holder.multi_view.setVisibility(View.VISIBLE);
            holder.rela_dynamic_resume.setVisibility(View.GONE);
            holder.rela_dynamic_job.setVisibility(View.GONE);

            if (TextUtils.isEmpty(item.topic.content)) {
                holder.tv_content.setVisibility(View.GONE);
                holder.tv_content.setText("");
            } else {
                holder.tv_content.setVisibility(View.VISIBLE);
                holder.tv_content.setText(item.topic.content);
            }
            holder.multi_view.setLayoutManager(new StaggeredGridLayoutManager(3, LinearLayout.VERTICAL));
            MultiTypeAdapter mAdapter = new MultiTypeAdapter(item.topic.pictures);
            mAdapter.register(String.class,new PictureBinder());
            holder.multi_view.setAdapter(mAdapter);
        } else if (type == 2) {
            holder.multi_view.setVisibility(View.GONE);
            holder.rela_dynamic_job.setVisibility(View.GONE);
            holder.rela_dynamic_resume.setVisibility(View.VISIBLE);
            holder.rela_dynamic_resume.setOnClickListener(this);
            holder.tv_content.setText("");
            holder.tv_content.setVisibility(View.VISIBLE);
            DymanicAllBean.Dymanic.Resume resumRes = item.resume;

            holder.tv_content.setText(resumRes.firstPublish == 0 ? "更新了求职简历" : "发布了求职简历");

            holder.tv_resume_user_name.setText(resumRes.name);

            GlideUtil.loadImg(context, resumRes.avatar, holder.img_resume_photo, R.drawable
                    .header_square);

            holder.tv_job_details.setText(StringUtils.info(" | ",resumRes.city,
                    resumRes.workYearsName, resumRes.degreeName));

            DymanicAllBean.Dymanic.Resume.WorkExperience work = resumRes.lastWorkExperience;
            holder.tv_resume_type.setText(StringUtils.info(" | ",resumRes
                    .titleName, resumRes.register));
            holder.tv_resume_experience.setText(work.startTime.replaceAll("-",
                    ".") + "-" + work.endTime.replaceAll("-", ".")
                    + "  " + TimeUtil.getTimeap(work.startTime, work.endTime) + "\n" + work.company + " " + work.position);
            holder.tv_resume_time.setText(TimeUtil.transferLongToDate(TimeUtil
                    .SIMPLEFORMATTYPESTRING8, item.topic.createTime));
        } else if (type == 3){
            holder.multi_view.setVisibility(View.GONE);
            holder.rela_dynamic_resume.setVisibility(View.GONE);
            holder.rela_dynamic_job.setVisibility(View.VISIBLE);
            holder.rela_dynamic_job.setOnClickListener(this);
            holder.tv_content.setText("");
            holder.tv_content.setVisibility(View.VISIBLE);
            DymanicAllBean.Dymanic.Recruitment recruitment = item.recruitment;
            if (recruitment != null) {
                holder.tv_content.setText(recruitment.firstPublish == 0 ? "更新了招聘职位" : "发布了招聘职位");

                DymanicAllBean.Dymanic.Recruitment.Company company = recruitment.company;

                GlideUtil.loadImg(context, company.logo, holder.img_job_photo,
                        R.drawable.morengongsilogo);

                holder.tv_job_name.setText(recruitment.positionName);
                String location;
                try {
                    location = recruitment.location.split(" ")[0];
                } catch (Exception e) {
                    e.printStackTrace();
                    location = "";
                }
                holder.tv_job_details.setText(StringUtils.info(location, recruitment
                                .workYearsName,
                        recruitment.degreeName, recruitment.titleName, recruitment.register));
                holder.tv_salary.setText(recruitment.salaryMin / 1000 + "K-" +
                        recruitment.salaryMax / 1000 + "K");


                if (recruitment.salaryType == 1) {
                    holder.tv_salary.setText(recruitment.salaryMin / 1000 + "K-" + recruitment.salaryMax / 1000 +
                            "K");
                } else {
                    holder.tv_salary.setText("基本工资" + recruitment.salaryMin / 1000 + "K-"
                            + recruitment.salaryMax / 1000 + "K\t+提成");
                }
                holder.tv_job_company.setText(StringUtils.info(company.shortName,company.typeName,company.sizeName));

                holder.tv_job_level.setText(company.industryName+"\t"+company.qualification);

                holder.tv_time.setText(TimeUtil.transferLongToDate(TimeUtil.SIMPLEFORMATTYPESTRING7,
                        recruitment.createTime));
            } else {
                holder.tv_content.setText("");
            }

        }


        if (item.commentList != null && item.commentList.size() > 0) {
            holder.view_line.setVisibility(View.VISIBLE);
            holder.recy_view_dis.setVisibility(View.VISIBLE);
            holder.recy_view_dis.setLayoutManager(new LinearLayoutManager(context));
            MultiTypeAdapter mAdapter = new MultiTypeAdapter(item.commentList.size()>3?item.commentList.subList(0,4):item.commentList);//最多显示三条
            mAdapter.register(DymanicAllBean.Dymanic.Comment.class,new CommentBinder());
            holder.recy_view_dis.setAdapter(mAdapter);
        } else {
            holder.recy_view_dis.setVisibility(View.GONE);
            holder.view_line.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {

    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        //头部 信息
        public ImageView img_header;
        public TextView tv_name;
        public TextView tv_company;
        //自我发布消息 可删除
        public TextView tv_date;
        public TextView tv_delete;
        //动态
        public TextView tv_content; //文字显示 职位与简历都可用
        public RecyclerView multi_view;
        //发布职位
        public RelativeLayout rela_dynamic_job;
        public ImageView img_job_photo;
        public TextView tv_job_name;
        public TextView tv_salary;
        public TextView tv_job_details;
        public TextView tv_job_company;
        public TextView tv_time;
        public TextView tv_job_level;
        //发布简历
        public RelativeLayout rela_dynamic_resume;
        public ImageView img_resume_photo;
        public TextView tv_resume_user_name;
        public TextView tv_resume_time;
        public TextView tv_resume_details;
        public TextView tv_resume_type;
        public TextView tv_resume_experience;
        //三个按钮
        public DynamicView dynamic_zan;
        public DynamicView dynamic_discuss;
        public DynamicView dynamic_share;
        //line
        public View view_line;
        //底部评论
        public RecyclerView recy_view_dis;

        public ViewHolder(View itemView) {
            super(itemView);
            //头部 信息
            img_header = itemView.findViewById(R.id.img_header);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_company = itemView.findViewById(R.id.tv_company);
            //删除自我发布消息
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_delete = itemView.findViewById(R.id.tv_delete);
            //动态
            tv_content = itemView.findViewById(R.id.tv_content);
            multi_view = itemView.findViewById(R.id.multi_view);
            //发布职位
            rela_dynamic_job = itemView.findViewById(R.id.rela_dynamic_job);
            img_job_photo = itemView.findViewById(R.id.img_job_photo);
            tv_job_name = itemView.findViewById(R.id.tv_job_name);
            tv_salary = itemView.findViewById(R.id.tv_salary);
            tv_job_details = itemView.findViewById(R.id.tv_job_details);
            tv_job_company = itemView.findViewById(R.id.tv_job_company);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_job_level = itemView.findViewById(R.id.tv_job_level);
            //发布简历
            rela_dynamic_resume =itemView.findViewById(R.id.rela_dynamic_resume);
            img_resume_photo = itemView.findViewById(R.id.img_resume_photo);
            tv_resume_user_name = itemView.findViewById(R.id.tv_resume_user_name);
            tv_resume_time = itemView.findViewById(R.id.tv_resume_time);
            tv_resume_details = itemView.findViewById(R.id.tv_resume_details);
            tv_resume_type = itemView.findViewById(R.id.tv_resume_type);
            tv_resume_experience = itemView.findViewById(R.id.tv_resume_experience);
            //三个按钮
            dynamic_zan = itemView.findViewById(R.id.dynamic_zan);
            dynamic_discuss = itemView.findViewById(R.id.dynamic_discuss);
            dynamic_share = itemView.findViewById(R.id.dynamic_share);
            //line
            view_line = itemView.findViewById(R.id.view_line);
            //底部评论
            recy_view_dis = itemView.findViewById(R.id.recy_view_dis);
        }
    }
}
